/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       bitmap_layer_window.cpp

	$Header: /game/bitmap_layer_window.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "bitmap_raw_window.h"

#include "bitmap_layer.h"
#include "paint_surface.h"
// -------------------------------------------------------------------
// constructor
// -------------------------------------------------------------------
t_bitmap_raw_window::t_bitmap_raw_window( t_bitmap_raw_ptr const& bitmap, t_screen_point point, 
											  t_window* parent, bool use_offset )
				     : t_window( k_no_transparency, parent ), m_bitmap( bitmap )
{
	if (bitmap == 0)
	{
		init( t_screen_rect( point.x, point.y, point.x, point.y ) );
		return;
	}

	t_screen_rect rect = bitmap->get_rect();

	if (!use_offset)
		rect -= rect.top_left();

	init( rect + point );
}

// --------------------------------------------------------
// change the bitmap
// --------------------------------------------------------
void t_bitmap_raw_window::set_bitmap( t_bitmap_raw_16 const* bitmap, bool use_offset )
{
	t_screen_point point = get_parent()->to_client( get_screen_rect().top_left() );

	if (use_offset)
	{
		if (m_bitmap != 0)
			point -= m_bitmap->get_rect().top_left();
	}
	else
	{
		if (bitmap != 0)
			point -= bitmap->get_rect().top_left();
	}

	set_bitmap( bitmap, point );
}

// --------------------------------------------------------
// change the bitmap
// --------------------------------------------------------
void t_bitmap_raw_window::on_bitmap_changed()
{
}

// --------------------------------------------------------
// change the bitmap
// --------------------------------------------------------
void t_bitmap_raw_window::set_bitmap( t_bitmap_raw_16 const* bitmap, t_screen_point point )
{
	if (bitmap == m_bitmap)
		return;

	t_screen_rect rect;
	bool          was_visible = is_visible();

	set_visible( false );
	m_bitmap = bitmap;

	if (bitmap == 0)
		rect = t_screen_rect( point.x, point.y, point.x, point.y );
	else
	{
		m_has_transparency = k_no_transparency;
		rect = bitmap->get_rect() + point;
	}

	on_bitmap_changed();
	move( rect );
	set_visible( was_visible );
	get_parent()->update_size();
}

// --------------------------------------------------------
// check if point is over the bitmap
// --------------------------------------------------------
bool t_bitmap_raw_window::is_contained( t_screen_point point ) const
{
	if (!is_point_in_rect( point, get_client_rect() ))
		return false;

	return true;
}

// -------------------------------------------------------------------
// paint the window
// -------------------------------------------------------------------
void t_bitmap_raw_window::paint( t_paint_surface& paint_surface )
{
	if (m_bitmap == 0)
		return;

	t_abstract_bitmap16_ptr	dest = paint_surface.get_bitmap();
	t_screen_rect           dest_rect = paint_surface.get_rect();
	t_screen_point          dest_point(0,0);
	t_screen_rect           rect = m_bitmap->get_rect();
	t_screen_rect           source_rect = dest_rect + rect.top_left();

	m_bitmap->draw_to( source_rect, *dest, dest_point );
}
